package org.qloudgen.borg.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.Cluster;

public class LocalShellExec {

	static Log log = LogFactory.getLog(LocalShellExec.class);
	
	public static int clusterOperator(Cluster cluster,String operatorType, String nodeIp){
		
		int ret = 0;
		
	    if (operatorType.equals(CommonValues.CLUSTER_ADD_NODE)
				||operatorType.equals(CommonValues.CLUSTER_REMOVE_NODE)){
		
			/* add/remove node */
	    	String[] command = {"sh", CommonValues.OPERATOR_SH,cluster.getMasterIp(),cluster.getSysUser(), 
	    			cluster.getSysUserPassword(),operatorType , nodeIp};
	    	ret = CommonUtils.execShellCommandWithReturn( command);
			
		} else if (operatorType.equals(CommonValues.CLUSTER_START)
				||operatorType.equals(CommonValues.CLUSTER_STOP)){
		
			//start/stop cluster
			String[] command = {"sh", CommonValues.OPERATOR_SH,cluster.getMasterIp(),cluster.getSysUser(), 
					cluster.getSysUserPassword(),operatorType };
			ret = CommonUtils.execShellCommandWithReturn( command );
			
		}

		return ret;
	}
	
}
